class JoininPanel extends BasePanel {
    public lab_roomid: eui.Label;
    private btn_0: eui.Button;
    private btn_1: eui.Button;
    private btn_2: eui.Button;
    private btn_3: eui.Button;
    private btn_4: eui.Button;
    private btn_5: eui.Button;
    private btn_6: eui.Button;
    private btn_7: eui.Button;
    private btn_8: eui.Button;
    private btn_9: eui.Button;
    private btn_cancel: eui.Button;
    private btn_confirm: eui.Button;

    protected selectNums: number[] = [];

    constructor() {
        super();

        this.skinName = "JoininPanelSkin";
    }

    childrenCreated() {
        super.childrenCreated();
        this.currentState = "normal";
        let arr: eui.Button[] = [
            this.btn_0,
            this.btn_1, this.btn_2, this.btn_3,
            this.btn_4, this.btn_5, this.btn_6,
            this.btn_7, this.btn_8, this.btn_9
        ];

        for (let i: number = 0; i < arr.length; i++) {
            arr[i].name = "" + i;
            arr[i].addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        }

        this.btn_cancel.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
        this.btn_confirm.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);
    }

    private clickHandler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_cancel:
                if (this.selectNums.length) this.selectNums.pop();
                break;
            case this.btn_confirm:
                if (this.selectNums.length < 4) {
                    EffectTextUtils.showTips("请输入4-6位的房间号", 5, false);
                    return;
                }
                this.onJoin();
                break;
            default:
                if (this.selectNums.length < 6) this.selectNums.push(e.currentTarget.name);
                break;
        }

        this.update();
    }

    protected onJoin() {
        game.roomid = Number(this.selectNums.join(""));
        gameManager.socketManager.send(3, {
            roomid: game.roomid,
            pass: "0",
            create_time: 0
        });
    }

    private update() {
        this.lab_roomid.text = this.selectNums.join("  ");
        this.lab_roomid.textColor = this.selectNums.length > 0 ? 0x9d3f2a : 0xc6cbc2;
    }

    show(): void {
        super.show();
        this.reset();
        
    }

    public reset() {
        this.selectNums = [];
        this.update();
    }


}